<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .w {
            margin: 10px auto;
            width: 1200px;
        }

        .slider-bar {
            position: absolute;
            display: block;
            background-color: #ccc;
            height: 180px;
            width: 100px;
            right: 133px;
            top: 340px;
        }

        .slider-bar .goback {
            display: none;
            background-color: #ccc;
        }

        .header {
            background-color: #aaa;
            height: 200px;
        }

        .banner {
            background-color: #888;
            height: 300px;
        }

        .main {
            background-color: #555;
            height: 800px;
        }
    </style>
</head>

<body>
    <div class="slider-bar">
        <span class="goback">返回顶部</span>
    </div>
    <div class="header w ">头部</div>
    <div class="banner w">banner</div>
    <div class="main w">主体</div>

</body>
<script>
    var sliderbar = document.querySelector('.slider-bar');
    var banner = document.querySelector('.banner');
    var goback = document.querySelector('.goback');
    var main = document.querySelector('.main');

    var botop = banner.offsetTop;
    var mtop = main.offsetTop;
    document.addEventListener('scroll', function () {

        console.log(pageYOffset);
        if (pageYOffset >= botop) {
            sliderbar.style.position = 'fixed';
            sliderbar.style.top = 340 - botop + 'px';
        }
        else {
            sliderbar.style.position = 'absolute';
            sliderbar.style.top = '340px';
        }
        if (pageYOffset >= mtop) {
            goback.style.display = 'block';
        } else {
            goback.style.display = 'none';
        }
    })
</script>

</html>